#include <algorithm>
#include <functional>
#include <vector>
using namespace std;

#define pg(g)do{\
                for(auto&i:(g)){\
                    for(auto&j:i){\
                        cout<<j<<",";\
                    }\
                    cout<<"\n";\
                }\
            }while(0)

class Solution {
public:
    vector<vector<int>> sortMatrix(vector<vector<int>>& g) {
        vector<int>p;
        for(int i=0;i<g.size();++i){
            int x=i,y=0;
            p.clear();
            do {
                p.push_back(g[x][y]);
                ++x,++y;
            }while (x<g.size()&&y<g[0].size());
            sort(p.begin(),p.end(),greater<>());
            x=i,y=0;
            int tot=0;
            do {
                g[x][y]=p[tot++];
                ++x,++y;
            }while (x<g.size()&&y<g[0].size());
        }
        // pg(g);
        for (int j=1; j<g[0].size(); ++j) {
            int x=0,y=j;
            p.clear();
            do{
                p.push_back(g[x][y]);
                ++x;++y;
            }while(x<g.size()&&y<g[0].size());
            sort(p.begin(),p.end());
            x=0,y=j;
            int tot=0;
            do {
                g[x][y]=p[tot++];
                ++x,++y;
            }while (x<g.size()&&y<g[0].size());
        }
        // pg(g);
        return g;
    }
};

int main(){
    Solution s;
    vector<vector<int>> v={{1,7,3},{5,8,2},{4,9,6}};
    s.sortMatrix(v);
}